package me.jzn.alib.custom.uri;

import android.net.Uri;

import androidx.annotation.NonNull;

/**
 * 因为资源名字可能被混淆，尽量不要使用这个
 * assets:///xxx
 * file:///android_asset/xxx
 */
public class AssetsUri implements BaseCustomUri {
    private Uri mUri;
    private String mPath;

    public AssetsUri(String assetsPath) {
        mUri = new Uri.Builder().scheme(BaseCustomUri.SCHEME_ASSETS).path(assetsPath).build();
        mPath = assetsPath;
    }

    //assets:///xxx
    //file:///android_asset/xxx
    public AssetsUri(Uri uri) {
        mUri = uri;
        String scheme = uri.getScheme();
        if (BaseCustomUri.SCHEME_ASSETS.equals(scheme)) {
            mPath = uri.getPath();
        } else if ("file".equals(scheme)) {
            String path = uri.getPath();
            int rawEndIdx = path.indexOf("/", 1);
            mPath = path.substring(rawEndIdx);
        }
    }

    public String getPath() {
        return mPath;
    }

    @Deprecated
    @NonNull
    @Override
    public String toString() {
        if (mUri == null) {
            throw new IllegalStateException("uri为空，请使用getType/getRes");
        }
        return mUri.toString();
    }

    @Override
    public String getScheme() {
        return mUri.getScheme();
    }
}
